package com.chilly.jdkNio.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 应用实例1：拷贝文件 transFrom方法
 * 1) 使用fileChannel 和方法 read write ，完成文件拷贝
 * 2） 拷贝一张图片
 * <p>
 * transferFrom(ReadableByteChannel src , long position, long count)
 * transferTo(long position, long count, WritableByteChannel target)
 * <p>
 * Created by Chilly Cui on 2021/1/9.
 */
public class NIOFileChannelOper04 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("c:\\env\\a.jpg");
        FileOutputStream out = new FileOutputStream("c:\\env\\a2.jpg");

        FileChannel source = in.getChannel();
        FileChannel dest = out.getChannel();

        //transFrom方法

        long l = dest.transferFrom(source, 0, source.size());

        //关闭流
        source.close();
        dest.close();
        in.close();
        out.close();

    }
}
